package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.VNSmartUI;
import vns.client.event.ObjectPropertyTypeEvent;
import vns.client.event.ObjectPropertyTypeEventHandler;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectPropertyTypeServiceAsync;
import vns.client.service.ObjectTypeService;
import vns.client.service.ObjectTypeServiceAsync;
import vns.client.view.ObjectPropertyTypeUI;
import vns.shared.ObjectPropertyTypeDTO;
import vns.shared.ObjectTypeDTO;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ObjectPropertyTypePresenter implements Presenter {

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display {
		Button getUpdateButton();

		Button getRemoveButton();

		ArrayList<ObjectPropertyTypeDTO> getListRow();

		boolean setListRow(List<ObjectPropertyTypeDTO> listRow);

		List<ObjectTypeDTO> getListOType();

		void setListOType(List<ObjectTypeDTO> listOType);

		Widget asWidget();

		void render();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final ObjectPropertyTypeServiceAsync rpcService;
	private final ObjectTypeServiceAsync rpcServiceOType;

	/*
	 * Public method
	 */

	public ObjectPropertyTypePresenter(ObjectPropertyTypeUI view,
			SimpleEventBus eventBus, ObjectPropertyTypeServiceAsync model) {
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService = model;
		this.rpcServiceOType = GWT.create(ObjectTypeService.class);
		bind();
	}

	/*
	 * Private method
	 */

	private boolean bind() {
		display.getUpdateButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		// Listen to even

		return false;
	}

	private void doInitTable() {

		new RPCCall<ArrayList<ObjectPropertyTypeDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không load được kiểu dữ liệu!"
						+ caught.toString());
			}

			@Override
			public void onSuccess(ArrayList<ObjectPropertyTypeDTO> result) {
				display.getListRow().addAll(result);
				new RPCCall<ArrayList<ObjectTypeDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Không load được danh sách !");
					}

					@Override
					public void onSuccess(ArrayList<ObjectTypeDTO> result) {
						display.setListOType(result);
						display.render();
					}

					@Override
					protected void callService(
							AsyncCallback<ArrayList<ObjectTypeDTO>> cb2) {
						rpcServiceOType.Loadall(cb2);

					}
				}.retry(3);
			}

			@Override
			protected void callService(
					AsyncCallback<ArrayList<ObjectPropertyTypeDTO>> cb) {
				rpcService.Loadall(cb);
			}

		}.retry(3);
	}

	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiLCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiLCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiYCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiYCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRimCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRizCAwBoolean
	// 0 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRizCAwBoolean

	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiMCAwDatetime
	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiMCAwDatetime
	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiZCAwDatetime
	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRinCAwDatetime
	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRi0CAwDatetime
	// 1 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRi0CAwDatetime
	//
	// 2 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiNCAwString
	// 2 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiNCAwString
	// 2 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRiaCAwString
	// 2 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRi1CAwString
	// 2 aglub19hcHBfaWRyGQsSEk9iamVjdFByb3BlcnR5VHlwZRi1CAwString

	private void doUpdate() {
		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi khi đang cập nhật! \n "
						+ caught.toString());
			}

			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công! \n ");
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				rpcService.UpdateAll(display.getListRow(), cb);
			}
		}.retry(3);
	}
}
